﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Hotel.DichVu;

namespace Hotel.DichVu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private SqlConnection con;
        private SqlDataAdapter da = new SqlDataAdapter();
        QL_dichvu quanly = new QL_dichvu();

        private Object ListDichVu()
        {
            String sql = "select * from KhoDichVu where DaXoa = 0 AND SoLuong IS NOT NULL";

            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private Object ListNhanVien()
        {
            String sql = "select * from NhanVien where DaXoa = 0";
            String sqlConnectString = "Data Source=localhost;Initial Catalog=Hotel;Integrated Security=True";
            SqlConnection con = new SqlConnection(sqlConnectString);
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private void btnNhapKhoReset_Click(object sender, EventArgs e)
        {
            //textBox1.ResetText();
            tbxNhapKhoNhapSoLuong.ResetText();
            //textBox3.ResetText();
            //textBox4.ResetText();
        }

        private void btnNhapKhoCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();

        }

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
      //  QL_dichvu q = new QL_dichvu();
        private void btnNhapKho_Click(object sender, EventArgs e)
        {
            if ((tbxNhapKhoNhapSoLuong.Text != ""))
            {
                int a = Convert.ToInt32(tbxNhapKhoNhapSoLuong.Text);
                serverdata("UPDATE KhoDichVu SET SoLuong=SoLuong+'" + a + "' WHERE TenHang=N'" + cbxNhapKhoChonTen.Text + "'");
               // quanly.Dispose();
                //quanly.QL_dichvu_Load(sender,e);
                MessageBox.Show("Nhập dịch vụ vào kho thành công!!!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Dispose();
                //QL_dichvu.khoDichVuTableAdapter.Fill(QL_dichvu.hotelDataSet.KhoDichVu);
               // quanly.Update();

            }
            else
            {
                MessageBox.Show("Mời nhập đầy đủ thông tin cho phiếu nhập kho", "Lỗi thiếu thông tin", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //quanly.QL_dichvu_Load(sender, e);

        }

        private void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi cơ sở dữ liệu!!!", "Lỗi cơ sở dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbxNhapKhoNhapSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !((e.KeyChar >= 48) && (e.KeyChar <= 57) || (e.KeyChar == 8));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cbxNhapKhoChonTen.DataSource = ListDichVu();
            cbxNhapKhoChonTen.DisplayMember = "TenHang";
            cbxNhapKhoChonTen.SelectedIndex = -1;
            cbxNhapKhoChonTenNV.DataSource = ListNhanVien();
            cbxNhapKhoChonTenNV.DisplayMember = "TenNV";
            cbxNhapKhoChonTenNV.SelectedIndex = -1;
           
        }
    }
}
